﻿using System;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using DeepEarth.Client.MapControl.Layers;

namespace DeepEarth.Client.Services.EarthAtlas
{
    public enum EarthAtlasModes
    {
        EarthLights,
        NaturalEarth2,
        NaturalEarth3
    }

    public class EarthAtlasTileSource : TileSource
    {
        // Fields
        private bool _isInitialized;
        private bool _isTileDownloadStarted;
        private EarthAtlasModes _mapMode;
        private string _maptype;
        private EventHandler InitializeCompleted;
        private const string TilePathBase = "http://earthatlas.info/maptiles/3785/{maptype}/{z}/{x}/{y}.png";

        // Methods
        public EarthAtlasTileSource()
        {
            this._maptype = string.Empty;
        }

        public EarthAtlasTileSource(EarthAtlasModes mode)
        {
            this._maptype = string.Empty;
            this.MapMode = mode;
        }

        public override UIElement GetCopyright()
        {
            return new Image { Source = new BitmapImage(new Uri("http://earthatlas.info/img/earthatlas_large.png")), MaxHeight = 48.0, MaxWidth = 96.0, Stretch = Stretch.Uniform };
        }

        public override Uri GetTile(int tileLevel, int tilePositionX, int tilePositionY)
        {
            if (this.IsInitialized)
            {
                int num = this.TileToZoom(tileLevel);
                this._isTileDownloadStarted = true;
                tilePositionY = (int)Math.Abs((double)(tilePositionY - (Math.Pow(2.0, (double)num) - 1.0)));
                string str = "http://earthatlas.info/maptiles/3785/{maptype}/{z}/{x}/{y}.png";
                return new Uri(str.Replace("{maptype}", this.MapMode.ToString().ToLower()).Replace("{z}", num.ToString()).Replace("{x}", tilePositionX.ToString()).Replace("{y}", tilePositionY.ToString()));
            }
            return null;
        }

        // Properties
        public override bool IsInitialized
        {
            get
            {
                return this._isInitialized;
            }
        }

        public EarthAtlasModes MapMode
        {
            get
            {
                return this._mapMode;
            }
            set
            {
                if (this._isTileDownloadStarted)
                {
                    throw new InvalidOperationException();
                }
                this._mapMode = value;
                base.Id = value.ToString();
                this._isInitialized = true;
                if (this.InitializeCompleted != null)
                {
                    this.InitializeCompleted(this, null);
                }
                EarthAtlasModes mapMode = this.MapMode;
            }
        }

        public override Color TileColor
        {
            get
            {
                return Colors.Gray;
            }
        }
    }


}
